function addOrEditBunddle(action,supid,updateId){
	var url = website + "boBunddle/insertPage";
	var params = null;
	var ids = null;
	if(action == ACTION_EDIT){
		ids = supid;
		params = {
			supid : ids,
			bunddleid : updateId,
		}
		url = website + "boBunddle/updatePage";
	}else{
		if(supid == null){
			ids = getSelectedItems("checkKey");
			if (ids == "" || ids.split(",").length != 1) {
				alert("请只选择一个商家");
				return;
			}
		}else{
			ids = supid;
		}
		params = {
			supid : ids
		}
	}
	
	$("#curAction").val(action);
	$("#curSupplier").val(ids);
	
	mask.open(true);
	ajaxGet(url, params, function(result) {
		$("#dialog-form-dish-bunddle-add").html(result);
		
		var name = $("#name");
		var price = $("#price");
		var file = $("#file");
		var desc = $("#desc");
		var allFields = $([]).add(name).add(price).add(file).add(desc), tips = $(".validateTips");
		$("#dialog-form-dish-bunddle-add").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					var bValid = true;
					allFields.removeClass("ui-state-error");
					bValid = bValid&& checkLength(name, "名称",1, 16, tips);
					bValid = bValid&& checkLength(price, "价钱",1, 16, tips);
					
					if(bValid){
						var params = {
							name:name.val(),
							price:price.val(),
							desc:desc.val(),
							supplierid:$("#curSupplier").val()
						}
						var url = website+"boBunddle/insert";
						if($("#curAction").val() == ACTION_EDIT){
							url = website+"boBunddle/update";
							params["id"] = $("#bunddle_edit_id").val();
						}
						mask.open(true);
						$.ajaxFileUpload
						(
							{
								url:url,
								secureuri:false,
								fileElementId:'file',
								dataType: 'html',
								data:params,
								success: function (data, status)
								{
									if(typeof(data.error) != 'undefined')
									{
										if(data.error != '')
										{
											alert(data.error);
										}else
										{
											alert(data.msg);
										}
									}else{
										showBunddle();
										$("#dialog-form-dish-bunddle-add").dialog("close");
									}
									mask.close();
								},
								error: function (data, status, e)
								{
									alert(data.responseText);
									mask.close();
								}
							}
						);
					}
				}
			}
		});
		mask.close();
	}, "html");
}

function showBunddleById(obj){
	$("#curSupplier").val(obj);
	showBunddle();
}

function showBunddle(){
	initBunddle($("#curSupplier").val());
}

function initBunddle(obj){
	var params = {
		supid : obj
	}
	mask.open(true);
	ajaxGet(website + "boBunddle/show", params, function(result) {
		$("#dialog-form-dish-bunddle").html(result);
		$("#dialog-form-dish-bunddle").dialog({
			width : 600,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).html("");
			}
		});
		$("#create-bunddle").button().click(function() {
			addOrEditBunddle(ACTION_ADD,$("#curSupplier").val());
		});
		$("#delete-bunddle").button().click(function() {
			deleteDialog(DISH_BUNDDLE, "checkSubKey");
		});
		mask.close();
	}, "html");
}

function editBunddle(obj){
	addOrEditBunddle(ACTION_EDIT,$("#curSupplier").val(),obj);
}

function showBunddleItem() {
	initBunddleItem($("#curBunddle").val());
}

function initBunddleItem(bunddleid) {
	var params = {
		bunddleid : bunddleid
	}
	mask.open(true);
	ajaxGet(website + "boBunddleItem/show", params, function(result) {
		$("#dialog-form-dish").html(result);
		$("#dialog-form-dish").dialog({
			width : 1000,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					showBunddle();
					$(this).dialog("close");
					$(this).html("");
				}
			}
		});
		initBunddleItemBtns(bunddleid);
		mask.close();
	}, "html");
}

function initBunddleItemBtns(bunddleid){
	$("#create-bunddle-item").button().click(function() {
		addOrEditBunddleItemDialog(ACTION_ADD,bunddleid);
	});
	$("#delete-bunddle-item").button().click(function() {
		deleteDialog(BUNDDLE_ITEM, "checkSubKey");
	});
}

function showDishByBunddle(obj){
	$("#curBunddle").val(obj);
	initBunddleItem(obj);
	$("#dialog-form-dish-bunddle").dialog("close");
}

function addOrEditBunddleItemDialog(action,id) {
	var params = null;
	var url = website + "boBunddleItem/insertPage";
	if(action == ACTION_ADD){
		params = {
			bunddleid : id
		};
	}else{
		params = {
			bunddleItemId : id
		};
		url = website + "boBunddleItem/updatePage";
	}
	$("#curAction").val(action);
	mask.open(true);
	ajaxGet(url, params, function(result) {
		$("#dialog-form-dish-add").html(result);
		
		var name = $("#name");
		var originalPrice = $("#originalPrice");
		var discountPrice = $("#discountPrice");
		var quantity = $("#quantity");
		var file = $("#file");
		var desc = $("#desc");
		var allFields = $([]).add(name).add(originalPrice).add(discountPrice).add(quantity).add(file).add(desc), tips = $(".validateTips");
		$("#dialog-form-dish-add").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					var bValid = true;
					allFields.removeClass("ui-state-error");
					bValid = bValid&& checkLength(name, "名称",1, 16, tips);
					bValid = bValid&& checkLength(originalPrice, "原价",1, 16, tips);
					bValid = bValid&& checkLength(discountPrice, "特价",1, 16, tips);
					bValid = bValid&& checkLength(quantity, "数量",1, 16, tips);

					if(bValid){
						var bunddleid = $("#curBunddle").val();
						var params = {
							name:name.val(),
							originalprice:originalPrice.val(),
							discountprice:discountPrice.val(),
							quantity:quantity.val(),
							desc:desc.val(),
							bunddleid:bunddleid,
							supplierid:$("#curSupplier").val()
						}
						var url = website+"boBunddleItem/insert";
						if($("#curAction").val() == ACTION_EDIT){
							url = website+"boBunddleItem/update";
							params["id"] = id;
						}
						mask.open(true);
						$.ajaxFileUpload
						(
							{
								url:url,
								secureuri:false,
								fileElementId:'file',
								dataType: 'html',
								data:params,
								success: function (data, status)
								{
									if(typeof(data.error) != 'undefined')
									{
										if(data.error != '')
										{
											alert(data.error);
										}else
										{
											alert(data.msg);
										}
									}else{
										$("#dialog-form-dish").html(data);
										initBunddleItemBtns(bunddleid);
										$("#dialog-form-dish-add").dialog("close");
									}
									mask.close();
								},
								error: function (data, status, e)
								{
									alert(data.responseText);
									mask.close();
								}
							}
						);
					}
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function editBunddleItem(obj){
	addOrEditBunddleItemDialog(ACTION_EDIT,obj);
}